var express =require('express');
var router = express.Router();
var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR="mongodb://localhost:27017/product";

router.get('/detail',function(req,res,next){
    async.parallel([function(cb){
         MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                var conn=db.collection('movie_detail');
                var data={id:req.query.id};
                conn.find(data).toArray(function(err,results){
                    cb(null,results);
                    db.close();
                })
            }
        })
    },function(cb){
         MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                var conn=db.collection('comment');
                var data={id:req.query.id};
                conn.find(data).toArray(function(err,results){
                    cb(null,results);
                    db.close();
                })
            }
        })
    }],function(err,results){
        res.render('movieDetail',{data:{
            det:results[0][0],
            comment:results[1],
            phone:req.session.phone,
        }});
        
    })
})

router.post('/comment',function(req,res,next){
    var content = req.body.content;
    var phone = req.session.phone;
    var id = req.body.imgid;
    if(phone){
        MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                var conn = db.collection('comment');
                var data = {phone:phone,content:content,id:id};
                conn.insert(data,function(err,results){
                res.redirect('/movie/detail?id='+id);
                db.close();
                })
            }
        })
        
    }else{
        res.send('<script>alert("session过期或者未登录"); location.href ="/login"</script>')
    }
})

router.get('/list',function(req,res,next){
    var table = req.query.collection;
    var pageNo = req.query.pageNo,
    pageNo = pageNo?pageNo:1,
    
    pageSize=6;
    count=0;
    totalPages =0;
    
    async.parallel([function(cb){
         MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                var conn=db.collection(''+table);
                var data={};
                conn.find(data).toArray(function(err,results){
                    totalPages=Math.ceil(results.length/pageSize);
                    count=results.length;
                    cb(null,results);
                    db.close();
                })
            }
        })
    },function(cb){
         MongoClient.connect(DB_CONN_STR,function(err,db){
            if(err){
                return;
            }else{
                var conn=db.collection(''+table);
                var data={};
                conn.find(data).skip((pageNo-1)*pageSize).limit(pageSize).toArray(function(err,results){
                    cb(null,results);
                    db.close();
                })
            }
        })
    }],function(err,results){
        res.render('list',{data:{
            pageNo:pageNo,
            totalPages:totalPages,
            table:table,
            list:results[1],
            phone:req.session.phone,
        }});
        
    })

})
module.exports= router;